﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace PixelWarfare
{
    /// <summary>
    /// The main game screen that draws the blob field as it grows.
    /// </summary>
    class GameScreen : Screen
    {
        #region Fields

        private static GameScreen theOne;   // Singleton instance.
        
        private bool finished;              // Whether the game has finished.
        private Game game;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new game, private so we can ensure there is only
        /// ever one instance at a time. The instance can be gotten from
        /// GetInstance().
        /// </summary>
        private GameScreen()
        {
            game = Game.GetInstance();
        }

        #endregion

        #region Public Methods


        /// <summary>
        /// Gets the singleton instance of the class.
        /// </summary>
        /// <returns>Return the singleton instance.</returns>
        public static GameScreen GetInstance()
        {
            // Create it if it doesnt already exist.
            if (theOne == null)
            {
                theOne = new GameScreen();
            }

            /* Since were using it as a singleton we need to update it
             * as unexpired everytime we want to show it again. 
             * Otherwise the screen would switch immediately. 
             */
            theOne.ValidateScreen();

            // Same as above except for the game.
            theOne.finished = false;

            return theOne;
        }
        

        /// <summary>
        /// Draw the map, blob field, bars, and the winner if the game is finished.
        /// </summary>
        /// <param name="g">Graphics to be drawn on.</param>
        public override void Draw(Graphics g)
        {
            g.DrawImage(game.GetMap(), 0, 0);
            g.DrawImage(game.GetFieldImage(), 0, 0);

            // Draw the outer bars for each player.
            for (int i = 0; i < Game.MaxPlayers; i++)
            {
                Utility.DrawBar(g, i, game.GetTotalPercentage(i), game.GetColor(i));
            }

            // If the game is finished then draw the winner.
            if (finished)
            {
                Utility.DrawTextCentered(g, game.GetWinnerName() + " WINS!", Color.White);
            }
        }

        /// <summary>
        /// Update the game if its not finished.
        /// </summary>
        public override void Update()
        {
            if (!finished)
            {
                finished = game.Update();
            }
        }

        /// <summary>
        /// Move to the score screen if the mouse is pressed after the game is over.
        /// </summary>
        /// <param name="x">X coordinate the mouse was pressed at.</param>
        /// <param name="y">Y coordinate the mouse was pressed at.</param>
        public override void MousePress(int x, int y)
        {
            if (finished)
            {
                InvalidateScreen(ScoreScreen.GetInstance());
            }
        }

        /// <summary>
        /// Handles all key input.
        /// </summary>
        /// <param name="key">Key that was pressed.</param>
        public override void KeyPress(Keys key)
        {
            switch (key)
            {
                case Keys.Escape:
                    InvalidateScreen(MainMenuScreen.GetInstance());
                    break;

                case Keys.Enter:
                    if (finished)
                    {
                        InvalidateScreen(ScoreScreen.GetInstance());
                    }
                    break;

            }
        }

        /// <summary>
        /// The GameScreen has no actions that need to be mapped to these inputs.
        /// </summary>
        #region Uneeded Methods

        public override void MouseMove(int x, int y) { }     

        public override void MouseRelease(int x, int y) { }

        public override void KeyRelease(Keys key) { }

        #endregion

        #endregion
    }
}
